package org.android.SuperList;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class DetalleLista extends ListActivity
{
	
    private static final int ACTIVITY_EDITAR=2;
    private static final int ACTIVITY_CREAR=1;
    private static final int ACTIVITY_CATALOGOS=2;
    private static final int MENU_INSERTAR = Menu.FIRST;
    private static final int MENU_ELIMINAR = Menu.FIRST + 1;
    private static final int MENU_CATALOGOS = Menu.FIRST + 3;
    private static final int MENU_CANCELAR = Menu.FIRST + 4;    
    private DbAdapter mDbHelper;
    private Long ID_LISTA;

    
    
    /** Called when the activity is first created. */
    
    protected void onCreate(Bundle savedInstaceState) 
    {    	
        super.onCreate(savedInstaceState);
       
        setContentView(R.layout.detallelista);
        mDbHelper= new DbAdapter(this);
        mDbHelper.open();
        
        registerForContextMenu(getListView()); 
        ID_LISTA = savedInstaceState != null ? savedInstaceState.getLong(DbAdapter.ID) :null;		
		if (ID_LISTA == null) 
		{
			Bundle extras = getIntent().getExtras();            
			ID_LISTA = extras != null ? extras.getLong(DbAdapter.ID): null;
		}
		CargarDatos();
		
    }
    
    public void CargarDatos()
    {
    	try
    	{
			Cursor ListaCursor=  mDbHelper.RecuperaRegistros(DbAdapter.VISTA_LISTADESGLOSADA,DbAdapter.ID_LISTA,ID_LISTA);
			startManagingCursor(ListaCursor);    	
			String[] Origen = new String[]{DbAdapter.GASTO,DbAdapter.MONTO,DbAdapter.FECHA};
			int [] Destino = new int[]{R.id.txvDetalleConcepto,R.id.txvDetalleMonto,R.id.txvDetalleFecha};    	
			SimpleCursorAdapter listas = new SimpleCursorAdapter(this,R.layout.registrodetallelista,ListaCursor,Origen,Destino);
			setListAdapter(listas);
    	}
    	catch(Exception Exc)
    	{
    		Log.e("Carga de Datos", "Error: "+Exc);
    	}
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
    	Intent i;
    	super.onListItemClick(l, v, position, id);
    	i = new Intent(this,EditaListaGastos.class);    
        i.putExtra(DbAdapter.ID, id);
        i.putExtra(DbAdapter.ID_LISTA,ID_LISTA);
        startActivityForResult(i, ACTIVITY_EDITAR);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_INSERTAR, 0, R.string.menu_insertar);
        menu.add(0, MENU_CATALOGOS, 1, R.string.menu_catalogos);
        return true;        
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) 
    {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, MENU_ELIMINAR, 0, R.string.menu_eliminar);        
        menu.add(0, MENU_CANCELAR, 1, R.string.menu_cancelar);
	}

    @Override
	public boolean onMenuItemSelected(int caracteristica,MenuItem item)
    { 
    	Intent i;
    	switch(item.getItemId())
    	{
    	case MENU_INSERTAR:
    		i = new Intent(this,EditaListaGastos.class);    		
    		i.putExtra(DbAdapter.ID_LISTA,ID_LISTA);
    		startActivityForResult(i,ACTIVITY_CREAR);
    		return true;
    	case MENU_CATALOGOS:
    		i = new Intent(this, Catalogos.class);            
            startActivityForResult(i, ACTIVITY_CATALOGOS);   		
    		return true;    		
    	}	
    	return super.onMenuItemSelected(caracteristica, item);
    } 
       
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent intent) 
    {
        super.onActivityResult(requestCode, resultCode, intent);
        CargarDatos();
    }
    
    @Override
	public boolean onContextItemSelected(MenuItem item) 
    {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch(item.getItemId()) 
		{
    	case MENU_ELIMINAR:    		
	        mDbHelper.eliminaRegistro(DbAdapter.TABLA_RELACIONESLISTASGASTOS,info.id);
	        CargarDatos();	        
	        return true;    	
		case MENU_CANCELAR:    		
			setResult(RESULT_OK);    	    
    		return true;
		}
		return super.onContextItemSelected(item);
	}
    
}
